How to do it?:

Submission: Submit the link on Github of the assignment to Canvas


  1. Install two packages gganimate and gifski then restart Rstudio. Using the Adult Census Income data, make an animation using geom_point and transition_states.
#install.packages('gganimate')
#install.packages('gifski')
library(gganimate)
library(ggplot2)
library(tidyverse)
library(knitr)
library(tidyverse)
setwd("C:/Users/student/Downloads")
df =  read_csv('adult_census.csv')
head(df)
## # A tibble: 6 × 15
##     age workclass fnlwgt education    education.num marital.status occupation   
##   <dbl> <chr>      <dbl> <chr>                <dbl> <chr>          <chr>        
## 1    90 ?          77053 HS-grad                  9 Widowed        ?            
## 2    82 Private   132870 HS-grad                  9 Widowed        Exec-manager…
## 3    66 ?         186061 Some-college            10 Widowed        ?            
## 4    54 Private   140359 7th-8th                  4 Divorced       Machine-op-i…
## 5    41 Private   264663 Some-college            10 Separated      Prof-special…
## 6    34 Private   216864 HS-grad                  9 Divorced       Other-service
## # ℹ 8 more variables: relationship <chr>, race <chr>, sex <chr>,
## #   capital.gain <dbl>, capital.loss <dbl>, hours.per.week <dbl>,
## #   native.country <chr>, income <chr>
df %>% ggplot(aes(x = age,
           y = hours.per.week, color=income))+
  geom_point()+
  transition_states(sex)+
  labs(title = 'Sex: {closest_state}')

  1. Using the Adult Census Income data, make an animation using geom_bar and transition_states.
df %>% ggplot(aes(x = sex,
                  fill=race))+
  geom_bar(position = 'fill')+
  transition_states(education) +
  labs(title = 'Education: {closest_state}')

  1. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19 in 2021.
df <- read.csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')

df$month = month(df$Date_reported)
df$year = year(df$Date_reported)

df <- df %>% filter(year==2021)
library(lubridate)
df$week <- week(df$Date_reported)
d1 <- df %>% group_by(month, Country) %>% summarise(sum = sum(New_deaths))
d2 <- d1 %>% group_by(month) %>% mutate(rank=rank(-sum)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=sum, group=Country, fill=Country, label=Country)) + geom_col()+
    geom_text(aes(y = sum, label = Country), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Month {closest_state}', x='', y='Total Number of New Deaths', fill='state')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(month)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)

  1. Make a bar race using a dataset of your own interest. You may use the dataset that we use in class (https://covidtracking.com/data/download/all-states-history.csv) but you should make a different bar racev from ones in the slides.
df <- read.csv('https://covidtracking.com/data/download/all-states-history.csv')
library(lubridate)
df$year <- year(df$date)
d1 <- df %>% group_by(year, state) %>% summarise(max = max(negative))
d2 <- d1 %>% group_by(year) %>% mutate(rank=rank(-max)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=max, group=state, fill=state, label=state)) + geom_col()+
    geom_text(aes(y = max, label = state), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'Year {closest_state}', x='', y='Total Number of Negative Caeses', fill='state')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(year)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)